#include "user_manage.h" //

// #define pft() {};//待定

char user_gfind_v[][30] = {"工号", "姓名", "手机号", "身份证号", "性别", "密码"};
char user_gscanf_matrl[][15] = {"%s", "%s", "%s", "%s", "%s"};

int (*user_func_array[5])(const void *dast, const void *str, size_t mod); // 函数数组

// 查询函数，mod = 1精确，= 0 模糊
void *user_find_data(vector v, void *data, int (*v_find)(const void *, const void *, size_t mod), size_t mod)
{
    // int i = 0;

    // while (i < v->size)
    // {
    //     if (0 == v_find(&v->buff[i], data, mod))
    //         return &v->buff[i];
    //     i++;
    // }

    // return NULL;

    int i = 0, j = 0;
    char rtn, c;
    if (!mod)
    {

        printf("-------(继续请回车(Q/q结束)------\n");
    }

    while (i < v->size)
    {
        if (0 == v_find(&v->buff[i], data, mod) && mod)
            return &v->buff[i];
        else
        {
            if (0 == v_find(&v->buff[i], data, mod))
            {
                user_show_inf(&v->buff[i]);

                j++;
                if (5 <= j)
                {
                    j = 0;

                    rtn = getchar();
                    while ('\n' != rtn && '\n' != (c = getchar())) // 一个回车也会进入下一循环
                        ;

                    if ('Q' == rtn || 'q' == rtn)
                    {
                        return NULL;
                    }
                    else
                    {
                        i++;
                        continue;
                    }
                }
                printf("\n");
            }
        }

        i++;
    }

    return NULL;
}

// 寻找ID
int user_find_dast_ID(const void *dast, const void *str, size_t mod)
{
    union elem_vector *temp = (union elem_vector *)dast;
    if (mod)
    {
        if (strlen(temp->_USER.ID) != strlen(str))
            return 1;

        if (0 == strcmp(temp->_USER.ID, str)) //((struct _role_inf *)dast)->ID == ((struct _role_inf *)str)->ID
            return 0;
    }
    else
    {
        if (NULL != strstr(temp->_USER.ID, str))
            return 0;
    }
    return 1;
}

// 寻找名字
int user_find_dast_name(const void *dast, const void *str, size_t mod)
{
    union elem_vector *temp = (union elem_vector *)dast;
    if (mod)
    {
        if (strlen(temp->_USER.name) != strlen(str))
            return 1;
        if (0 == strcmp(temp->_USER.name, str)) //((struct _role_inf *)dast)->ID == ((struct _role_inf *)str)->ID
            return 0;
    }
    else
    {
        if (NULL != strstr(temp->_USER.name, str))
            return 0;
    }
    return 1;
}

// 寻找手机号
int user_find_dast_phone(const void *dast, const void *str, size_t mod)
{

    union elem_vector *temp = (union elem_vector *)dast;
    if (mod)
    {
        if (strlen(temp->_USER.phone) != strlen(str))
            return 1;
        if (0 == strcmp(temp->_USER.phone, str)) //((struct _role_inf *)dast)->ID == ((struct _role_inf *)str)->ID
            return 0;
    }
    else
    {
        if (NULL != strstr(temp->_USER.phone, str))
            return 0;
    }
    return 1;
}

// 寻找身份证号
int user_find_dast_ID_card(const void *dast, const void *str, size_t mod)
{

    union elem_vector *temp = (union elem_vector *)dast;
    if (mod)
    {
        if (strlen(temp->_USER.ID_card) != strlen(str))
            return 1;
        if (0 == strcmp(temp->_USER.ID_card, str)) //((struct _role_inf *)dast)->ID == ((struct _role_inf *)str)->ID
            return 0;
    }
    else
    {
        if (NULL != strstr(temp->_USER.ID_card, str))
            return 0;
    }
    return 1;
}

int user_find_dast_md5_passwd(const void *dast, const void *str, size_t mod)
{

    union elem_vector *temp = (union elem_vector *)dast;

    if (strlen(temp->_USER.md5_passwd) != strlen(str))
        return 1;
    if (0 == strcmp(temp->_USER.md5_passwd, str)) //((struct _role_inf *)dast)->ID == ((struct _role_inf *)str)->ID
        return 0;

    return 1;
}

void user_Vector_Find_Init(void) // 存放需要调用的函数数组
{
    user_func_array[0] = user_find_dast_ID;
    user_func_array[1] = user_find_dast_name;
    user_func_array[2] = user_find_dast_phone;
    user_func_array[3] = user_find_dast_ID_card;
    user_func_array[4] = user_find_dast_md5_passwd;
}

// 数据显示
void user_show_inf(elem_type *data)
{
    printf("%s：%s, ", user_gfind_v[0], data->_USER.ID);
    printf("%s：%s, ", user_gfind_v[1], data->_USER.name);
    printf("%s：%s, ", user_gfind_v[2], data->_USER.phone);
    printf("%s：%s, ", user_gfind_v[3], data->_USER.ID_card);
    printf("%s：%s。", user_gfind_v[4], data->_USER.sex);
}

void user_Vector_Find(vector v) // 在链表数据里查找数据
{
    int open, prefuzzy;
    char YN_continue;
    char c;
    elem_type *temp;
    void *data = (void *)malloc(sizeof(elem_type));

    user_Vector_Find_Init();

    // system("clear");

    while (1)
    {
        system("clear");

        printf("\n---------用户信息查询-----------\n");

        int i = 0;
        for (i = 0; i < PROJECT_NUM - 1; i++)
        {
            printf("\n%d.通过%s查询", i + 1, user_gfind_v[i]); // 打印选项
        }

        printf("\n0.返回上一级菜单\n");
        printf("\n:> ");

        if (scanf("%d", &open) != 1)
        {
            while ('\n' != (c = getchar()))
                ;
        }
        if (0 == open)
        {
            return;
        }

        if (open < 0 || open > PROJECT_NUM - 1)
        {
            printf("输入错误\n");
            continue;
        }
        prefuzzy = open;

        while (1)
        {
            printf("1.精确\n2.模糊\n0.返回上一步::>");

            if (scanf("%d", &open) != 1)
            {
                while ('\n' != (c = getchar()))
                    ;
            }

            if (0 == open)
            {
                break;
            }

            if (1 != open && 2 != open)
            {
                printf("输入错误\n");
                continue;
            }

            printf("请输入%s:", user_gfind_v[prefuzzy - 1]);
            scanf(user_gscanf_matrl[prefuzzy - 1], data); // 数据放入堆区空间

            while ('\n' != (c = getchar()))
                ;
            if (NULL != (temp = user_find_data(v, data, user_func_array[prefuzzy - 1], 2 - open)))
            {
                printf("该用户信息为:\n");
                user_show_inf(temp);
            }
            else if (2 == open)
            {
                printf("end\n");
            }
            else
            {
                printf("no find\n");
            }
            printf("是否继续通过%s查询(Y/N)？\n", user_gfind_v[prefuzzy - 1]); // 打印选项
            YN_continue = getchar();
            while ('\n' != (c = getchar()))
                ;
            if (YN_continue == 'N' || YN_continue == 'n')
                break;

            system("clear");
            // 传入
        }

        // printf("\n是否退出(Y/N)？");

        // YN_continue = getchar();
        // while ('\n' != (c = getchar()))
        //     ;

        // if (YN_continue == 'Y' || YN_continue == 'y')
        //     break;
    }

    free(data);

    // system("clear");

    return;
}
